﻿/************************************************************************************************************
* file    : DailyReportController.cs
* author  : RDSSH
* function: 
* history : created by RDSSH 2019/1/25 10:25:25
************************************************************************************************************/
using IStrong.EC.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using IStrong.EC.WebApi.Results.Entity.Daily;
using IStrong.EC.WebApi.Results.Entity.ViewModel;
using IStrong.EC.WebApi.Results.Repository;
using Senparc.Weixin.MP.TenPayLibV3;

namespace IStrong.EC.WebApi.Results.Controllers
{
    /// <summary>
    /// Api控制器模板
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class DailyReportController : BaseController
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<DailyReportController> _logger;

        /// <summary>
        /// 配置
        /// </summary>
        private readonly IConfiguration _config;

        private readonly IMapper _mapper;

        private readonly DailyReportRepository _dailyReportRepository;

        private readonly WeixinusersRepository _weixinuserRepository;

        /// <summary>
        /// 注入服务 
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="logger"></param>
        /// <param name="config"></param>
        public DailyReportController(ILogger<DailyReportController> logger, IConfiguration config, IMapper mapper,
            DailyReportRepository dailyReportRepository, WeixinusersRepository weixinuserRepository)
        {
            _logger = logger;
            _config = config;
            _mapper = mapper;
            _dailyReportRepository = dailyReportRepository;
            _weixinuserRepository = weixinuserRepository;
        }

        [HttpGet]
        public async Task<JsonResult> Get(string OpenId, DateTime ReportDate)
        {
            List<UserDailyReportViewModel> Result = new List<UserDailyReportViewModel>();

            // 获取所有用户
            var users = _weixinuserRepository.GetUserInfos(new { OpenId });
            //获取当天所有日报
            var userReports = _dailyReportRepository.GetDailyReport(new { OpenId, ReportDate });
            foreach (var user in users)
            {
                UserDailyReportViewModel uDailyReportViewModel = new UserDailyReportViewModel();
                var uReports = from ur in userReports where ur.Openid == user.openid select ur;
                var todays = from ur in uReports where ur.Timetype == 0 select ur;
                var tomorrows = from ur in uReports where ur.Timetype == 1 select ur;
                uDailyReportViewModel.userinfo = user;
                uDailyReportViewModel.todays = _mapper.Map<IEnumerable<DailyReportViewModel>>(todays);
                uDailyReportViewModel.tommorrows = _mapper.Map<IEnumerable<DailyReportViewModel>>(tomorrows);
                Result.Add(uDailyReportViewModel);
            }

            return await Task.FromResult(Json(Result));
        }

        [HttpPost]
        public async Task<JsonResult> Post([FromBody] Dailyreports report)
        {
            var Result = _dailyReportRepository.AddDailyReport(report);
            return await Task.FromResult(Json(Result));
        }

        [HttpPut]
        public async Task<JsonResult> Put([FromBody] Dailyreports report)
        {
            var Result = _dailyReportRepository.UpdateDailyReport(report);
            return await Task.FromResult(Json(Result));
        }

        [HttpDelete]
        public async Task<JsonResult> Delete([FromBody]Dailyreports report)
        {
            var Result = _dailyReportRepository.DeleteDailyReport(new { id = report.Id });
            return await Task.FromResult(Json(Result));
        }


    }
}
